﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverTile;
using System.Linq;
using SilverTile.Xaml;
using SilverTile.Sprites;

namespace SilverTileApp
{
    public class MonsterSprite : AnimatedSprite
    {

        protected Direction _direction = Direction.None;

        public MonsterSprite(int width, int height)
            : base(width,height)
        {
            //_viewport = viewport;
            //AddAnimation(resources, "Left", "MonsterLeft1", "MonsterLeft2");
            //AddAnimation(resources, "Right", "MonsterRight1", "MonsterRight2");
            //AddAnimation(resources, "Up", "MonsterUp1", "MonsterUp2");
            //AddAnimation(resources, "Down", "MonsterDown1", "MonsterDown2");
            SelectRandomDirection();
      
        }

        public void SelectRandomDirection()
        {
            _direction = RandomDirection();
            SetActiveAnimation(_direction);
        }

        private Direction RandomDirection()
        {
            switch (GameImplementation.Rnd.Next(0, 4))
            {
                case 0:
                    return Direction.Up;
                case 1:
                    return Direction.Right;
                case 2:
                    return Direction.Down;
                default:
                    return Direction.Left;
            }
        }

        public void SetActiveAnimation(Direction direction)
        {
            SetActiveAnimation(direction.ToString());
        }

        public override void Update(TimeSpan timeSinceLastFrame)
        {

            Direction newDirection = _direction;
            Collision collision = WillCollideIfMoving(newDirection);

            while (collision.Collides() || GameImplementation.Rnd.Next(100) == 0)
            {
                newDirection = RandomDirection();
                collision = WillCollideIfMoving(newDirection);                
            }

            if (newDirection != _direction)
            {
                SetDirection(newDirection);
            }
            
            int dx = 0;
            int dy = 0;
            switch (_direction)
            {
                case Direction.Up:
                    dy = -1;
                    break;
                case Direction.Right:
                    dx = 1;
                    break;
                case Direction.Down:
                    dy = 1;
                    break;
                case Direction.Left:
                    dx = -1;
                    break;
            }

            X += dx;
            Y += dy;

            base.Update(timeSinceLastFrame);

        }

        private void SetDirection(Direction newDirection)
        {
            _direction = newDirection;
            SetActiveAnimation(newDirection);
        }

        protected Collision WillCollideIfMoving(Direction direction)
        {

            int dx = 0;
            int dy = 0;
            switch (direction)
            {
                case Direction.Up:
                    dy = -1;
                    break;
                case Direction.Right:
                    dx = 1;
                    break;
                case Direction.Down:
                    dy = 1;
                    break;
                case Direction.Left:
                    dx = -1;
                    break;
            }

            Collision collides = Viewport.SpriteWillCollideWithBackground(this, X + dx, Y + dy, -1);
            if (X + dx < 0)
            {
                collides = collides.Set(Collision.Left);
            }
            else if (X + dx >= Viewport.Width)
            {
                collides = collides.Set(Collision.Right);
            }

            if (Y + dy < 0)
            {
                collides = collides.Set(Collision.Top);
            }
            else if (Y + dy >= Viewport.Height)
            {
                collides = collides.Set(Collision.Bottom);
            }


            return collides;
        }

    }
}
